import java.util.LinkedList;
import java.util.Random;
import javax.swing.*;
import java.awt.*;


class she {
	
	private final int SIZE = 40;
	private final int COUNTS = 15;
	private final int BTNSIZE = 15;
	private int direction = 1; //1234 right,down,left,up;
	private LinkedList<JButton> python =  new LinkedList<>();
	private JButton[] food = new JButton[COUNTS];
	private int[][] sta = new int[COUNTS][2];
	private JFrame f = new JFrame("贪吃蛇");
	private JLabel jbs = new JLabel("碰到自己，游戏结束！");
	private JLabel jbw = new JLabel("撞到墙，游戏结束！");

	private int pyth_X = 20;
	private int pyth_Y = 20;
	
	
	public she(){
		init();
	}

	private void init(){
		initFood();
		initPython();
		initFrame();
	}

	private void initFrame(){
		jbs.setBounds(250,280,150,30);
		jbs.setFont(new Font("微软雅黑",1,22));
		jbs.setForeground(Color.RED);
		jbw.setBounds(250,280,150,30);
		jbw.setFont(new Font("微软雅黑",1,22));
		jbw.setForeground(Color.RED);
		f.setLayout(null);
		f.setSize(600, 600);
		f.setVisible(true);
		f.requestFocus();
		f.addKeyListener(new key(this));
	}

	private void initPython(){
		for(int i=0;i<3;i++){
			JButton btn = new JButton(i+"");
			btn.setBounds(pyth_X*BTNSIZE, pyth_Y*BTNSIZE, BTNSIZE, BTNSIZE);
			pyth_X--;
			python.addLast(btn);
			f.add(python.getLast());
			btn.setBackground(Color.BLUE);
		}
	}

	private void initFood(){
		Random r = new Random();
		for(int i=0;i<COUNTS;i++){
			int x = r.nextInt(40);
			int y = r.nextInt(40);
			while(y==20 || x==20 || x==19 || x==18){
				x = r.nextInt(40);
			    y = r.nextInt(40);
			}
			sta[i][0] = x;
			sta[i][1] = y;
			food[i] = new JButton("G");
			food[i].setBounds(sta[i][0]*BTNSIZE, sta[i][1]*BTNSIZE, BTNSIZE, BTNSIZE);
			f.add(food[i]);
			food[i].setBackground(Color.RED);
		}
	}
	
	public void ctrlDir(int s){
		if(Math.abs(this.direction-s)==2) return;
		this.direction = s;
	}
	
	private boolean isMeetItSelf(){

		int x = python.getFirst().getX();
		int y = python.getFirst().getY();

		for(int i=1;i<python.size();i++){
			if(x==python.get(i).getX() && y==python.get(i).getY()) {
				return true;
			}
		}

		return false;
	}
	
	private boolean isEat(){
		JButton btn = python.getFirst();
		int x = btn.getX();
		int y = btn.getY();
		int i;
		boolean flag = false;
		for(i=0;i<COUNTS;i++){
			if(x==sta[i][0]*BTNSIZE && y==sta[i][1]*BTNSIZE){
				flag = true;
				break;
			} 
		}
		if(!flag) return flag;
		//i--;
		Random r = new Random(System.currentTimeMillis());
		int x0,y0,j;
		while(true){
			x0 = r.nextInt(40)*BTNSIZE;
			y0 = r.nextInt(40)*BTNSIZE;
			for(j=0;j<COUNTS;j++){
				if(x0==sta[j][0]*BTNSIZE && y0==sta[j][1]*BTNSIZE) break;
				if(x==x0 && y==y0) break;
			}
			if(j>COUNTS-1) break;
		}
		food[i].setBounds(x0,y0,BTNSIZE,BTNSIZE);
		sta[i][0] = x0/BTNSIZE;
		sta[i][1] = y0/BTNSIZE;
		return flag;
	}
	
	private boolean isBreakWall(){
		JButton btn = python.getFirst();
		int x = btn.getX();
		int y = btn.getY();
		if(x<0 || x>(SIZE-1)*BTNSIZE || y<0 || y>(SIZE-1)*BTNSIZE){
			return true;
		} 
		return false;
	}
	
	private void addBody(){
		JButton btn = new JButton("G");
		JButton b1 = python.get(python.size()-1);
		JButton b2 = python.get(python.size()-2);
		int x,y;
		if(b1.getX()==b2.getX()){
			x = b1.getX();
			y = b2.getY()+BTNSIZE;
		}
		else{
			x = b2.getX()+BTNSIZE;
			y = b1.getY();
		}
		btn.setBounds(x,y,BTNSIZE,BTNSIZE);
		System.out.println("fffffffff");
		python.add(btn);
		f.add(btn);
		btn.setBackground(Color.BLACK);
	}
	
	protected boolean move(){
		if(isBreakWall()) return false;
		if(isMeetItSelf()) return false;
		if(isEat()) addBody();
		JButton last = python.pollLast();
		JButton first = python.getFirst();
		int x = first.getX();
		int y = first.getY();
		switch(direction){
			case 1: x = x+BTNSIZE;
				break;
			case 2: y = y+BTNSIZE;
				break;
			case 3: x = x-BTNSIZE;
				break;
			case 4: y = y-BTNSIZE;
				break;
		}
		last.setBounds(x,y,BTNSIZE,BTNSIZE);
		python.addFirst(last);
		System.out.println(x+"..."+y);
		return true;
	}

	protected void sleep(int s){
		try
		{
			Thread.sleep(s);

		}
		catch(Exception e){

		}
	}
	
	public static void main(String[] args){
		she t = new she();
		new runing(t).start();
		
	}
	
}


